#include <cmath>
#include "gui/objects/segments/GUIRoadSegment.h"
#include "util/MathUtils.h"

namespace gui {

GUIRoadSegment::GUIRoadSegment(RoadSegment* roadSegment) {
	Intersection* o = roadSegment->getOrigin();
	Intersection* d = roadSegment->getDestination();
	// 0px de ancho para que tome el ancho de la imagen.
	this->setDimensionsAndLocation(0, o, d);
}

GUIRoadSegment::GUIRoadSegment(double width, Intersection* origin, Intersection* destination) {
	this->setDimensionsAndLocation(width, origin, destination);
}

GUIRoadSegment::~GUIRoadSegment() {

}

void GUIRoadSegment::setDimensionsAndLocation(double width, Intersection* origin, Intersection* destination) {
	// TODO ver centrado u offset.
	// solo calculado asi por fines practicos para ver si estaba dibujando algo
	//this->setOffsetInMeters(origin->getX() > destination->getX() ? destination->getX() : origin->getX(),
	//		origin->getY() > destination->getY() ? destination->getY() : origin->getY());
	this->setCenterInMeters((origin->getX() + destination->getX()) / 2.0, (origin->getY() + destination->getY()) / 2.0);

	this->setSizeInMeters(width, this->getRoadSegmentLength(origin, destination));
	double angle = atan2((destination->getY() - origin->getY()) , (destination->getX() - origin->getX())) + PI / 2;
	this->setRotation(angle);
}

double GUIRoadSegment::getRoadSegmentLength(Intersection* o, Intersection* d) {
	double x = fabs(d->getX() - o->getX());
	double y = fabs(d->getY() - o->getY());
	return sqrt(x * x + y * y);
}

}
